﻿using NPOI.HSSF.UserModel;
using NPOI.SS.Converter;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    public class ExcelHelper
    {
        public static void ExcelToPdf(string filePath)
        {
        }

        private static void FormatSheet(ISheet sheet, ExcelSheetModel sheetModel)
        {
            Dictionary<string, ExcelMergeCellModel> dictionary = new Dictionary<string, ExcelMergeCellModel>();
            decimal num = 0M;
            int num2 = 0;
            List<ExcelRowModel> list = new List<ExcelRowModel>();
            int index = 0;
            while (true)
            {
                if (index >= sheet.NumMergedRegions)
                {
                    int rownum = 0;
                    while (true)
                    {
                        if (rownum > sheet.LastRowNum)
                        {
                            List<decimal> list2 = new List<decimal>();
                            int columnIndex = 0;
                            while (true)
                            {
                                if (columnIndex > num2)
                                {
                                    sheetModel.Rows = (IEnumerable<ExcelRowModel>)list;
                                    sheetModel.TableWidth = num;
                                    sheetModel.CellWidths = (IEnumerable<decimal>)list2;
                                    return;
                                }
                                decimal num8 = (decimal)widthToPixels((double)sheet.GetColumnWidth(columnIndex));
                                if (num8 == 0M)
                                {
                                    num8 = new decimal(0x48);
                                }
                                num = (decimal)(num + num8);
                                list2.Add(num8);
                                columnIndex++;
                            }
                        }
                        ExcelRowModel item = new ExcelRowModel();
                        List<ExcelCellModel> list3 = new List<ExcelCellModel>();
                        IRow row = sheet.GetRow(rownum);
                        if (row == null)
                        {
                            ExcelCellModel model5 = new ExcelCellModel
                            {
                                Text = "",
                                ColumnSpan = 1,
                                RowSpan = 1
                            };
                            list3.Add(model5);
                        }
                        else
                        {
                            int cellnum = 0;
                            while (true)
                            {
                                if (cellnum >= row.LastCellNum)
                                {
                                    break;
                                }
                                ExcelCellModel model3 = new ExcelCellModel();
                                ICell cell = row.GetCell(cellnum);
                                if (cell == null)
                                {
                                    model3.Text = "";
                                    model3.ColumnSpan = 1;
                                    model3.RowSpan = 1;
                                    list3.Add(model3);
                                }
                                else
                                {
                                    IWorkbook workbook = sheet.Workbook;
                                    model3.Alignment = cell.CellStyle.Alignment;
                                    model3.VerticalAlignment = cell.CellStyle.VerticalAlignment;
                                    model3.BorderTop = cell.CellStyle.BorderTop;
                                    model3.BorderRight = cell.CellStyle.BorderRight;
                                    model3.BorderBottom = cell.CellStyle.BorderBottom;
                                    model3.BorderLeft = cell.CellStyle.BorderLeft;
                                    if (cell.CellStyle is XSSFCellStyle)
                                    {
                                        XSSFCellStyle cellStyle = (XSSFCellStyle)cell.CellStyle;
                                        if ((cellStyle.TopBorderXSSFColor == null) || !cellStyle.TopBorderXSSFColor.IsAuto)
                                        {
                                            model3.BorderTopColor = ExcelToHtmlUtils.GetHexColor(workbook, cell.CellStyle.TopBorderColor);
                                        }
                                        if ((cellStyle.RightBorderXSSFColor == null) || !cellStyle.RightBorderXSSFColor.IsAuto)
                                        {
                                            model3.BorderRightColor = ExcelToHtmlUtils.GetHexColor(workbook, cell.CellStyle.RightBorderColor);
                                        }
                                        if ((cellStyle.BottomBorderXSSFColor == null) || !cellStyle.BottomBorderXSSFColor.IsAuto)
                                        {
                                            model3.BorderBottomColor = ExcelToHtmlUtils.GetHexColor(workbook, cell.CellStyle.BottomBorderColor);
                                        }
                                        if ((cellStyle.LeftBorderXSSFColor == null) || !cellStyle.LeftBorderXSSFColor.IsAuto)
                                        {
                                            model3.BorderLeftColor = ExcelToHtmlUtils.GetHexColor(workbook, cell.CellStyle.LeftBorderColor);
                                        }
                                    }
                                    else
                                    {
                                        model3.BorderTopColor = ExcelToHtmlUtils.GetHexColor(workbook, cell.CellStyle.TopBorderColor);
                                        model3.BorderRightColor = ExcelToHtmlUtils.GetHexColor(workbook, cell.CellStyle.RightBorderColor);
                                        model3.BorderBottomColor = ExcelToHtmlUtils.GetHexColor(workbook, cell.CellStyle.BottomBorderColor);
                                        model3.BorderLeftColor = ExcelToHtmlUtils.GetHexColor(workbook, cell.CellStyle.LeftBorderColor);
                                    }
                                    model3.WrapText = cell.CellStyle.WrapText;
                                    model3.HeightInPoints = (row.HeightInPoints == 13.5) ? 0f : row.HeightInPoints;
                                    model3.FillColor = ExcelToHtmlUtils.GetHexBgColor(workbook, cell.CellStyle);
                                    if (workbook != null)
                                    {
                                        model3.Font = cell.CellStyle.GetFont(sheet.Workbook);
                                        model3.FontColor = ExcelToHtmlUtils.GetHexFontColor(workbook, model3.Font);
                                    }
                                    model3.Text = GetCellText(cell, cell.CellType, model3);
                                    if (cell.CellStyle.BorderDiagonal != BorderDiagonal.None)
                                    {
                                        model3.Text = FormatText(model3.Text);
                                    }
                                    if (!cell.IsMergedCell)
                                    {
                                        model3.ColumnSpan = 1;
                                        model3.RowSpan = 1;
                                        list3.Add(model3);
                                    }
                                    else
                                    {
                                        string str2 = ((int)rownum).ToString() + "." + ((int)cellnum).ToString();
                                        if (dictionary.ContainsKey(str2))
                                        {
                                            ExcelMergeCellModel model4 = dictionary[str2];
                                            model3.ColumnSpan = model4.ColumnSpan;
                                            model3.RowSpan = model4.RowSpan;
                                            list3.Add(model3);
                                        }
                                    }
                                }
                                if (cellnum > num2)
                                {
                                    num2 = cellnum;
                                }
                                cellnum++;
                            }
                        }
                        item.Cells = (IEnumerable<ExcelCellModel>)list3;
                        list.Add(item);
                        rownum++;
                    }
                }
                CellRangeAddress mergedRegion = sheet.GetMergedRegion(index);
                ExcelMergeCellModel model1 = new ExcelMergeCellModel();
                model1.RowSpan = (mergedRegion.LastRow - mergedRegion.FirstRow) + 1;
                model1.ColumnSpan = (mergedRegion.LastColumn - mergedRegion.FirstColumn) + 1;
                model1.FirstRowIndex = mergedRegion.FirstRow;
                model1.LastRowIndex = mergedRegion.LastRow;
                model1.FirstColumnIndex = mergedRegion.FirstColumn;
                model1.LastColumnIndex = mergedRegion.LastColumn;
                ExcelMergeCellModel model = model1;
                int firstColumn = mergedRegion.FirstColumn;
                string key = ((int)mergedRegion.FirstRow).ToString() + "." + ((int)firstColumn).ToString();
                dictionary.Add(key, model);
                index++;
            }
        }

        private static string FormatText(string text)
        {
            string str4;
            if (string.IsNullOrEmpty(text))
            {
                str4 = "";
            }
            else
            {
                string oldValue = text.Trim();
                str4 = text.Replace(oldValue, "") + oldValue.Replace(" ", "  ");
            }
            return str4;
        }

        private static string GetCellText(ICell cell, CellType cellType, ExcelCellModel cellm)
        {
            cellm.CellType = cellType;
            string stringCellValue = "";
            switch (cellType)
            {
                case CellType.Unknown:
                    stringCellValue = cell.StringCellValue;
                    break;

                case CellType.Numeric:
                    {
                        string dataFormatString = cell.CellStyle.GetDataFormatString();
                        stringCellValue = (dataFormatString != "General") ? (!DateUtil.IsCellDateFormatted(cell) ? ((double)cell.NumericCellValue).ToString(dataFormatString) : cell.DateCellValue.ToString(dataFormatString)) : ((double)cell.NumericCellValue).ToString();
                        if ((dataFormatString == null) && (cell.CellStyle.DataFormat == 0x39))
                        {
                            stringCellValue = cell.DateCellValue.ToString("yyyy\"年\"M\"月\"");
                        }
                        break;
                    }
                case CellType.String:
                    stringCellValue = cell.StringCellValue;
                    break;

                case CellType.Formula:
                    stringCellValue = GetCellText(cell, cell.CachedFormulaResultType, cellm);
                    break;

                case CellType.Blank:
                    stringCellValue = "";
                    break;

                case CellType.Boolean:
                    {
                        string text1 = cell.BooleanCellValue.ToString();
                        string text3 = text1;
                        if (text1 == null)
                        {
                            string local1 = text1;
                            text3 = "";
                        }
                        stringCellValue = text3;
                        break;
                    }
                case CellType.Error:
                    {
                        string text2 = ((byte)cell.ErrorCellValue).ToString();
                        string text4 = text2;
                        if (text2 == null)
                        {
                            string local2 = text2;
                            text4 = "";
                        }
                        stringCellValue = text4;
                        break;
                    }
                default:
                    break;
            }
            return stringCellValue;
        }

        public static string ToHtml(string filePath)
        {
            string str;
            StringBuilder builder = new StringBuilder();
            FileStream stream = new FileStream(filePath, (FileMode)FileMode.Open, (FileAccess)FileAccess.Read);
            ISheet sheetAt = null;
            int numberOfSheets = 0;
            List<ExcelSheetModel> list = new List<ExcelSheetModel>();
            try
            {
                if (filePath.IndexOf(".xlsx") == -1)
                {
                    HSSFWorkbook workbook = new HSSFWorkbook((Stream)stream);
                    numberOfSheets = workbook.NumberOfSheets;
                    int index = 0;
                    while (true)
                    {
                        if (index >= numberOfSheets)
                        {
                            break;
                        }
                        sheetAt = workbook.GetSheetAt(index);
                        ExcelSheetModel model1 = new ExcelSheetModel();
                        model1.Id = "sheet" + ((int)index).ToString();
                        model1.Name = sheetAt.SheetName;
                        ExcelSheetModel sheetModel = model1;
                        FormatSheet(sheetAt, sheetModel);
                        list.Add(sheetModel);
                        index++;
                    }
                }
                else
                {
                    XSSFWorkbook workbook2 = new XSSFWorkbook((Stream)stream);
                    numberOfSheets = workbook2.NumberOfSheets;
                    int index = 0;
                    while (true)
                    {
                        if (index >= numberOfSheets)
                        {
                            break;
                        }
                        sheetAt = workbook2.GetSheetAt(index);
                        ExcelSheetModel model3 = new ExcelSheetModel();
                        model3.Id = "sheet" + ((int)index).ToString();
                        model3.Name = sheetAt.SheetName;
                        ExcelSheetModel sheetModel = model3;
                        FormatSheet(sheetAt, sheetModel);
                        list.Add(sheetModel);
                        index++;
                    }
                }
                builder.Append(HtmlHelper.GetExcelHtml((IEnumerable<ExcelSheetModel>)list));
                str = builder.ToString();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                stream.Close();
                stream.Dispose();
            }
            return str;
        }

        private static int widthToPixels(double widthUnits)
        {
            return (int)Math.Round((double)((widthUnits * 9.0) / 256.0));
        }
    }
}
